//
//  RootViewController.swift
//  微博Swift
//
//  Created by  justinchou on 16/2/23.
//  Copyright © 2016年  justinchou. All rights reserved.
//

import UIKit
import HMRefresh

class RootViewController: UIViewController {
    
    // MARK:- 监听方法
    @objc private func loginSuccess() {
        view = nil
        
        NSNotificationCenter.defaultCenter().removeObserver(self, name: JustinLoginSuccessNotification, object: nil)
    }

    /// 表格视图
    var tableView: UITableView?
    /// 访客视图
    var visitorView: VisitorView?
    /// 下拉刷新控件
    var refreshControl: HMRefreshControl?
    
    // MARK:- 生命周期
    override func loadView() {
        view = UIView()
        
        UserAccount.sharedUserAccount.isLogin ? setupTableView() : setupVisitorView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //如果是登录就加载数据
        if UserAccount.sharedUserAccount.isLogin {
            loadData()
        } else {
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "loginSuccess", name: JustinLoginSuccessNotification, object: nil)
        }

    }
    
    func loadData() {
        
    }

}

// MARK:- 实现代理方法
extension RootViewController: VisitorViewDelegate {

    func visitorViewDidLogin() {
        print("登录")
        let oauth = OAuthViewController()
        
        let nav = UINavigationController(rootViewController: oauth)
        
        self.presentViewController(nav, animated: true, completion: nil)
    }
    
    func visitorViewDidRegister() {
        print("注册")
    }
}

// MARK:- 界面设置
extension RootViewController {
    
    func setupVisitorView() {
        
        visitorView = VisitorView()
        view.addSubview(visitorView!)
        
        visitorView?.snp_makeConstraints(closure: { (make) -> Void in
            make.edges.equalTo(view)
        })
        
        visitorView?.delegate = self
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .Plain, target: self, action: "visitorViewDidLogin")
        navigationItem.rightBarButtonItem?.tintColor = UIColor.orangeColor()
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "visitorViewDidRegister")
        navigationItem.leftBarButtonItem?.tintColor = UIColor.orangeColor()
        
    }
    
    /// 加载表单
    func setupTableView() {
        
        tableView = UITableView(frame: CGRectZero, style: .Plain)
        view.addSubview(tableView!)
        
        tableView?.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        
        navigationItem.rightBarButtonItem = nil
        navigationItem.leftBarButtonItem = nil
        
        tableView?.dataSource = self
        tableView?.delegate = self
        
    }
    
    /// 设置刷新控件
    func setupRefreshControl() {
        
        guard let tableView = tableView else {
            return
        }
        
        if refreshControl != nil {
            return
        }
        
        refreshControl = HMRefreshControl()
        tableView.addSubview(refreshControl!)
        
        refreshControl?.pulldownView = CZPulldownRefreshView.refreshView()
        refreshControl?.normalString = "下拉起飞"
        refreshControl?.pullingString = "放开起飞"
        refreshControl?.refreshingString = "正在起飞"
        refreshControl?.lastRefreshString = "上次"
        
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
    }
}
// MARK:- 数据源和代理方法
extension RootViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
}
